﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GIVisualEditor
{
    public partial class StateViewControlCircular : UserControl  //TextBox
    {
        public const int BUTTONDOWN  = 0xA1;
        public const int CAPTION = 2;
        private Node associatedNode = null;
        private DirectedGraph graph = null;
        private Color borderColor = Color.Black;

        public StateViewControlCircular(DirectedGraph g, Node n)
        {
            this.graph = g;
            associatedNode = n;
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.Opaque, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.BackColor = Color.Transparent;
            this.BorderStyle = BorderStyle.None;

            this.Paint += new PaintEventHandler(StateViewControl_Paint);
            this.MouseDown += new MouseEventHandler(StateViewControl_MouseDown);
            this.GotFocus += new EventHandler(StateViewControl_GotFocus);
            this.LostFocus += new EventHandler(StateViewControl_LostFocus);
            this.LocationChanged += new EventHandler(StateViewControl_LocationChanged);
            this.MouseHover += new EventHandler(StateViewControl_MouseHover);
            this.MouseLeave += new EventHandler(StateViewControl_MouseLeave);
            this.Move += new EventHandler(StateViewControl_Move);
            this.Load += new EventHandler(StateViewControl_Load);
        }

        void StateViewControl_Load(object sender, EventArgs e)
        {
            this.txtText.Text = (this.associatedNode.Pattern.Length > 15) ? this.associatedNode.Pattern.Substring(0, 15) : this.associatedNode.Pattern;
        }

        void StateViewControl_Move(object sender, EventArgs e)
        {
            if (this.Parent != null)
            {
                this.Parent.Invalidate();
            }
            else
            {
                this.Invalidate();
            }
            
        }

        void StateViewControl_MouseLeave(object sender, EventArgs e)
        {
            borderColor = Color.Black;
            this.Invalidate();
        }

        void StateViewControl_MouseHover(object sender, EventArgs e)
        {
            borderColor = Color.LightBlue;
            this.Invalidate();
            
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }

        void StateViewControl_Paint(object sender, PaintEventArgs e)
        {
            //SetControlDimension();
            //Point p1 = GetCornerPosition(startPointCornerIndex), p2 = GetCornerPosition(endPointCornerIndex);
            //Brush brush = Brushes.Black;
            Pen pen = new Pen(borderColor, 1.5f);
            //Pen pen = new Pen(this.ForeColor, 2.0f);
            //System.Drawing.Drawing2D.AdjustableArrowCap myArrow = new System.Drawing.Drawing2D.AdjustableArrowCap(5, 8, true);

            //pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
            //pen.CustomStartCap = myArrow;

            e.Graphics.DrawEllipse(pen, new Rectangle(1, 1, this.Width-2, this.Height-2));
        }

        void StateViewControl_LocationChanged(object sender, EventArgs e)
        {
            new Rectangle(0, 0, this.Width, this.Height);
            if (this.graph != null) this.graph.ReDraw();
        }


        void StateViewControl_LostFocus(object sender, EventArgs e)
        {
            // check if shift button is pressed, if not then reset the selection state
            if (Control.ModifierKeys == Keys.Shift)
            {
                if (graph != null && graph.TaskBag != null && associatedNode != null)
                {
                    int i = graph.TaskBag.FindIndex(N => N == associatedNode);
                    if (i >= 0)
                    {
                        //this.Text = "Do nothing";// DO nothing
                    }
                    else
                    {
                        graph.TaskBag.Add(associatedNode);
                    }
                }
            }
        }

        void StateViewControl_GotFocus(object sender, EventArgs e)
        {
            if (Control.ModifierKeys == Keys.Shift)
            {
                if (graph != null && graph.TaskBag != null && associatedNode != null)
                {
                    int i = graph.TaskBag.FindIndex(N => N == associatedNode);
                    if (i >= 0)
                    {
                        //this.Text = "Do nothing";// DO nothing
                    }
                    else
                    {
                        graph.TaskBag.Add(associatedNode);
                    }
                }
                //else this.Text = "No Action";
            }
            else
            {
                //this.Text = "No Shift";
            }

        }





        void StateViewControl_MouseDown(object sender, MouseEventArgs e)
        {
            if( e.Button == System.Windows.Forms.MouseButtons.Left){
                this.Capture = false;
                Message msg  = Message.Create(this.Handle, BUTTONDOWN, new IntPtr(CAPTION), IntPtr.Zero);
                this.DefWndProc(ref msg);
            }
        }

    }
}
